﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Domains;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.AdminRoles
{
    public partial class EditRole : ManageBasePage
    {
        private IAdminModulTask _adminModulTask;
        private IAdminPowerTask _adminPowerTask;
        private IAdminMenuTask _adminMenuTask;
        private IAdminRoleTask _adminRoleTask;
        private AdminRole _roleInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("EditAdminRole");
            _adminModulTask = GetInstance<IAdminModulTask>();
            _adminMenuTask = GetInstance<IAdminMenuTask>();
            _adminPowerTask = GetInstance<IAdminPowerTask>();
            _adminRoleTask = GetInstance<IAdminRoleTask>();


            var roleId = 0;
            if (Request.QueryString["Id"] != null && Int32.TryParse(Request.QueryString["Id"], out roleId))
            {
                _roleInfo = _adminRoleTask.Get(roleId);
                if (_roleInfo == null)
                {
                    ShowMsgAndReturn("参数错误");
                    return;
                }
                if (!IsPostBack)
                {

                    lblRoleName.Text = _roleInfo.RoleName;
                    lblRoleId.Text = _roleInfo.Id.ToString();

                    BindRolePowers();
                    BindRoleMenus();
                }
            }
        }

        #region 绑定菜单列表

        private void BindRoleMenus()
        {
            var modulList = _adminModulTask.GetAll();
            rptMenuModuls.DataSource = modulList;
            rptMenuModuls.DataBind();
        }

        protected void rptMenuModuls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var lblModulId = e.Item.FindControl("lblModulId") as Label;
                var rptMenus = e.Item.FindControl("rptMenus") as Repeater;
                var adminModul = _adminModulTask.Get(Convert.ToInt32(lblModulId.Text));
                rptMenus.DataSource = adminModul.AdminMenus;
                rptMenus.DataBind();
            }
        }

        protected void rptMenus_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var lblMenuId = e.Item.FindControl("lblMenuId") as Label;
                var chkHas = e.Item.FindControl("chkHas") as CheckBox;
                chkHas.Checked = _roleInfo.AdminMenus.Any(m => m.Id == Convert.ToInt32(lblMenuId.Text));
            }
        }

        #endregion

        #region 绑定权限列表

        private void BindRolePowers()
        {
            var modulList = _adminModulTask.GetAll();
            rptRolePowerModuls.DataSource = modulList;
            rptRolePowerModuls.DataBind();
        }

        protected void rptRolePowerModuls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var lblPowerModulId = e.Item.FindControl("lblPowerModulId") as Label;
                var rptPowers = e.Item.FindControl("rptPowers") as Repeater;
                var adminModul = _adminModulTask.Get(Convert.ToInt32(lblPowerModulId.Text));
                rptPowers.DataSource = adminModul.AdminPowers;
                rptPowers.DataBind();
            }
        }

        protected void rptPowers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var lblPowerId = e.Item.FindControl("lblPowerId") as Label;
                var chkHas = e.Item.FindControl("chkHas") as CheckBox;
                chkHas.Checked = _roleInfo.AdminPowers.Any(m => m.Id == Convert.ToInt32(lblPowerId.Text));
            }
        }

        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var powerIdList = (from RepeaterItem item in rptRolePowerModuls.Items
                               where item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item
                               select item.FindControl("rptPowers") as Repeater
                                   into rptPowers
                                   from RepeaterItem powerItem in rptPowers.Items
                                   where
                                       powerItem.ItemType == ListItemType.AlternatingItem ||
                                       powerItem.ItemType == ListItemType.Item
                                   let lblPowerId = powerItem.FindControl("lblPowerId") as Label
                                   let chkHas = powerItem.FindControl("chkHas") as CheckBox
                                   where chkHas.Checked
                                   select Convert.ToInt32(lblPowerId.Text)
                              ).ToList();

            var menuIdList = (from RepeaterItem item in rptMenuModuls.Items
                              where item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item
                              select item.FindControl("rptMenus") as Repeater
                                  into rptMenus
                                  from RepeaterItem menuItem in rptMenus.Items
                                  where menuItem.ItemType == ListItemType.AlternatingItem ||
                                        menuItem.ItemType == ListItemType.Item
                                  let lblMenuId = menuItem.FindControl("lblMenuId") as Label
                                  let chkHas = menuItem.FindControl("chkHas") as CheckBox
                                  where chkHas.Checked
                                  select Convert.ToInt32(lblMenuId.Text)
                             ).ToList();

            var roleId = Convert.ToInt32(lblRoleId.Text);
            using (var work = UnitOfWork.Begin())
            {
                if (_adminRoleTask.EditRoleMenus(roleId, menuIdList) != null
                    && _adminRoleTask.EditRolePowers(roleId, powerIdList) != null)
                {
                    work.Commit();
                    ShowMsgAndReturn("保存角色信息成功");
                }
                else
                {
                    ShowMsg("保存角色信息失败");
                }
            }
        }

    }
}